using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EcsSiteDSTableAdapters;
using System.ComponentModel;

/// <summary>
/// Summary description for EcsOfficeCL
/// </summary>
/// 
namespace SiteECSWeb
{
    [DataObject]
    public class EcsOfficeCL
    {
        private EcsOfficeBLL _EcsOfficAPI = null;

        protected EcsOfficeBLL API
        {
            get
            {
                if (_EcsOfficAPI == null)
                {
                    _EcsOfficAPI = new EcsOfficeBLL();
                }
                return _EcsOfficAPI;
            }

        }


        private object GetCacheItem(string RawKey)
        {
            return HttpRuntime.Cache[GetCacheKey(RawKey)];
        }
        private readonly string[] MasterCacheKeyArray = { "EcsOfficesCache" };
        private string GetCacheKey(string cachekey)
        {
            return string.Concat(MasterCacheKeyArray[0], "-", cachekey);
        }



        const double CacheDuration = 60.0;
        //private void AddCacheItem(string RawKey, object value)
        //{
        //    HttpRuntime.Cache.Insert(GetCacheKey(RawKey), value, null, DateTime.Now.AddSeconds(CacheDuration), System.Web.Caching.Cache.NoSlidingExpiration);
        //}

        private void AddCacheItem(string rawKey, object value)
        {
            System.Web.Caching.Cache DataCache = HttpRuntime.Cache;

            // Make sure MasterCacheKeyArray[0] is in the cache - if not, add it
            if (DataCache[MasterCacheKeyArray[0]] == null)
                DataCache[MasterCacheKeyArray[0]] = DateTime.Now;

            // Add a CacheDependency
            System.Web.Caching.CacheDependency dependency = new System.Web.Caching.CacheDependency(null, MasterCacheKeyArray);
            DataCache.Insert(GetCacheKey(rawKey), value, dependency, DateTime.Now.AddSeconds(CacheDuration), System.Web.Caching.Cache.NoSlidingExpiration);
        }



        #region Select


        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public EcsSiteDS.EcsOfficDataTable GetOffices()
        {
            const string RawKey = "EcsOffices";
            // see if the item cahed
            EcsSiteDS.EcsOfficDataTable EcsOffice = GetCacheItem(RawKey) as EcsSiteDS.EcsOfficDataTable;

            if (EcsOffice == null)
            {
                // item not found in cache - Retrieve it and insert it into the cahe 
                EcsOffice = API.GetAllEcsOffice();
                AddCacheItem(RawKey, EcsOffice);
            }
            return EcsOffice;
        }

        #endregion

    }
}
